package com.zhongwang.mms.module.basic.controller;


import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.module.basic.model.Org;
import com.zhongwang.mms.module.basic.service.DictCommonService;
import com.zhongwang.mms.module.basic.service.OrgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.zhongwang.mms.constant.Constant.GROUP_POST;


/**
 * @Description: 部门接口
 *
 * 权限：
 * basic:org:view 查看
 * basic:org:edit 编辑
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/basic/org")
@SuppressWarnings("all")
public class OrgController extends BaseController {

    @Autowired
    private OrgService orgService;
    @Autowired
    private DictCommonService dictCommonService;
    @GetMapping("/page")
    @RequiresPermissions({"basic:org:view"})
    public String page(Model model) {
        model.addAttribute("posts", dictCommonService.findDictByGroupName(GROUP_POST));
        return "basic/org/page";
    }

    /**
     * 系统部门数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:org:view"})
    public List<Org> list(Org org) {
        return orgService.findOrgByCondition(org);
    }

    /**
     * 部门名称唯一验证
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"basic:org:view"})
    public ValidResult checkUnique(Org org) {
        Org m = orgService.findOrgByName(org.getName());
        if (org.getId() == null) {
            return ValidResult.result(m);
        }
        if (m == null || org.getId().equals(m.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建部门
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:org:edit"})
    public JsonResult save(Org org) {
        if(orgService.saveOrg(org)!=null){
            return success("保存成功！");
        }
        return error("保存失败！");
    }

    /**
     * 修改部门
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:org:edit"})
    public JsonResult update(Org org) {
        return isNotNull(orgService.updateOrg(org));
    }

    /**
     * 删除部门
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:org:edit"})
    public JsonResult del(Integer id) {
        return jsonResult(orgService.delOrg(id));
    }

}
